<template>
  <div class="employeeDetail" v-loading="loading">
    <section class="section">
      <div class="user-info">
        <div class="left">
          <div class="avatar">
            <img v-if="info.headPortrait" class="avatar" :src="info.headPortrait" alt />
            <div v-else class="default">{{ info.name && info.name.slice(-2) }}</div>
          </div>
          <div class="info">
            <div class="info1">
              <span class="name">{{ info.name }}</span>
              <span>NO.{{ info.jobno }}</span>
            </div>
            <div class>
              <span>职位：{{ info.postname || "--" }}</span>
              <span style="margin-left: 30px">部门：{{ info.departmentName }}</span>
            </div>
          </div>
        </div>
        <div class="oper">
          <div
            class="btn"
            @click="$router.push({ name: 'editEmployee', query: { isType: 'edit', employeeId: employeeId, code: tab, randomNum: Math.floor(Math.random() * 900) + 100 } })"
          >编辑</div>
        </div>
      </div>
      <div class="nav">
        <div
          v-for="(item, index) in management"
          :key="index"
          @click="tab = item.modulecode"
          class="tab-item"
          :class="{ 'tab-item-active': tab === item.modulecode }"
        >{{ item.modulename }}</div>
      </div>
    </section>
    <section class="section section1">
      <div class="bar">
        <div class="line"></div>
        <div class="text">{{ titleBar }}</div>
      </div>
      <div class="form" v-show="!formList.isList && formList.moudleTypeCode !== 1009">
        <div class="item" v-for="(item, index) in formList.moudleData[0].fields" :key="index">
          <div class="label">{{ item.columnname || item.columnName }}：</div>
          <div class="value">{{ item.value || "" }}</div>
        </div>
      </div>
      <div class="table" v-show="formList.isList">
        <el-table
          :header-cell-style="{ 'font-size': '13px', color: '#666666 !important' }"
          :data="tableDataObj[this.tab]"
          style="width: 100%"
        >
          <el-table-column
            :prop="item.columnid"
            :label="item.columnname"
            v-for="(item, index) in formList.moudleData[0].fields"
            :key="index"
          >
            <template slot-scope="scope">
              <span
                @click="lookFile(item, scope)"
                v-if="item.datatype == 11"
                style="cursor: pointer; color: #51acee; text-decoration: underline"
              >查看附件</span>
              <span v-else>{{ scope.row[item.columnid] ? scope.row[item.columnid] : "--" }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="piccm" v-show="formList.moudleTypeCode == 1009">
        <div class="pic-tab">
          <el-tabs v-model="picTab">
            <el-tab-pane
              :label="item.name"
              :name="index + ''"
              v-for="(item, index) in mateArr"
              :key="index"
            >
              <div slot="label" class="label_title">
                <img style="width: 25px; margin-left: 5px" :src="item.icon" alt />
                <span style="margin-left: 5px">{{ item.name }}</span>
              </div>
            </el-tab-pane>
          </el-tabs>
          <!-- <div class="pic-tab-item" :class="{ 'pic-tab-item-active': picTab === index }" @click="picTab = index" v-for="(item, index) in mateArr" :key="index">
            <img :src="item.icon" alt="" />
            <div class="txt">{{ item.name }}</div>
          </div>-->
        </div>
        <div class="item">
          <template v-if="srcList && srcList.length > 0">
            <el-image
              v-for="(item, index) in srcList"
              style="width: 145px; height: 98px; margin-right: 20px"
              :key="index"
              :preview-src-list="srcList"
              :src="item"
              fit="contain"
            >
              <template #error>
                <div class="error">暂无相关材料</div>
              </template>
            </el-image>
          </template>
          <div v-else class="noneImg">
            <img style="width: 50px" src="@/assets/images/personnel/zanwushangchuan.svg" alt />
            <span>暂无上传</span>
          </div>
        </div>
      </div>
    </section>
    <imagePerview :imagePerview="imagePerview" @close="imagePerview = false" :fileList="fileList"></imagePerview>
  </div>
</template>

<script>
import { mapState } from "vuex";
import sfz from "../../../assets/images/personnel/shenfenzheng.svg";
import xl from "@/assets/images/personnel/xuelizhengshu.svg";
import xw from "@/assets/images/personnel/xueweizhengshu.svg";
import qt from "@/assets/images/personnel/qita.svg";
import imagePerview from "@/components/imagePreview.vue";
let proMap = new Map(),
  cityMap = new Map(),
  quMap = new Map();
export default {
  name: "employeeDetail",
  // props: ["employeeId"],
  components: {
    imagePerview
  },
  computed: {
    ...mapState({
      management: state => state.personnel.management,
      companySection: state => state.common.companySection,
      provinceCityList: state => state.common.provinceCityList,
      YeepayBank: state => state.personnel.YeepayBank
    }),
    titleBar() {
      return this.management.find(i => this.tab === i.modulecode).modulename;
    },
    formList() {
      // let item = this.dataAll.find((i) => this.tab === i.moudleTypeCode) || { moudleData: [{}] };
      // if (item && item.moudleTypeCode == 1006) {
      //   let tempData = [];
      //   for (let i = 0; i < item.moudleData[0].fields.length; i++) {
      //     if(item.moudleData[0].fields[i].value) {
      //       tempData.push();
      //     }
      //   }
      // }
      return this.dataAll.find(i => this.tab === i.moudleTypeCode) || { moudleData: [{}] };
    },
    srcList() {
      return this.mateArr[this.picTab]?.value;
    }
  },
  data() {
    return {
      imagePerview: false,
      fileList: [{}],
      tab: 1001,
      picTab: 0,
      dataAll: [],
      loading: false,
      info: {},
      tableDataObj: {},
      mateArr: [],
      employeeId: ""
    };
  },
  methods: {
    lookFile(item, row) {
      console.log(item, row);
      let fileList = this.formList.moudleData[row.$index].fields.find(m => m.columnid == item.columnid)?.files;
      if (fileList && fileList.length > 0) {
        this.fileList = fileList;
        this.imagePerview = true;
      } else {
        this.$message.warning("暂无附件");
      }
    },
    handleData(data) {
      data.map(i => {
        i.proID && i.proRemark == null && proMap.set(i.proID, i);
        i.cityID && i.proID && cityMap.set(i.cityID, i);
        i.id && i.disName && quMap.set(i.id, i);
        i.children && this.handleData(i.children);
      });
    },
    cityCodeToName(str) {
      if (!str) return "";
      const [proID, cityID, id] = str.split(",").map(i => Number(i));
      const [p, c, q] = [proMap.get(proID), cityMap.get(cityID), quMap.get(id)];
      return `${p.name}/${c.name}${q ? "/" + q.name : ""}`;
    },
    async getData() {
      this.loading = true;
      const treeToArray = data => data.reduce((p, c) => p.concat(c, c.childrens), []);
      const comArr = treeToArray(this.companySection);
      const { data } = await this.$api.employeeApi.GetOneAll({ employeeId: this.employeeId });
      const {
        data: { data: info }
      } = await this.$api.optionsManage.GetEmployeePost({ employeeId: this.employeeId });
      let tableDataObj = {};
      let mateArr = [];
      let pic = [];

      data.data.map(i => {
        let tableData = [];
        i.moudleData.map(j => {
          let obj = {};
          j.fields.map((k, idx) => {
            // if(k.fieldItems) {
            //   j.fields.splice(idx + 1, 0, ...k.fieldItems);
            // }
            if (k.datatype == 2 && (k.columnname == "试用期" || k.columnname == "合同期限")) {
              let value = "";
              value += k.value == "0" ? "" : k.value;
              let tempValue = k.fieldItems[0].options.find(m => m.optionvalue == k.fieldItems[0].value)?.optiondisplay;
              value += tempValue ? tempValue : "";
              k.value = value;
            }
            if (k.datatype == 6) {
              //地区级联
              k.value = this.cityCodeToName(k.value);
            }
            if (k.datatype === 7 && k.columnname == "部门") {
              //多级部门id转名称
              k.value = k.value.includes(",") ? k.value.split(",").slice(-1)[0] : k.value;
              k.value = comArr.find(l => l.id === k.value)?.name || "";
            }
            if (k.datatype === 5 || k.datatype === 3) {
              //下拉id转名称
              k.value = k.options.find(l => l.optionvalue == k.value)?.optiondisplay || "";
            }
            if (k.datatype === 10 && k.columnname == "合同公司") {
              //公司id转名称
              k.value = comArr.find(l => l.id === k.value)?.name || "";
            }
            if (k.datatype === 8) {
              //时间
              if (k.value) {
                const date = new Date(k.value);
                k.value = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
              }
            }
            if (k.datatype === 12) {
              //个人材料
              if (k.columnname.includes("身份证")) {
                pic.push(k.value);
              } else {
                mateArr.push({ name: k.columnname, value: k.value ? [k.value] : [], icon: k.columnname.includes("学历") ? xl : xw });
              }
            }
            if (k.datatype === 13) {
              //个人材料
              mateArr.push({ name: k.columnname, value: k.files ? k.files.map(i => i.fileUrl) : [], icon: qt });
            }
            if (k.datatype === 14 && k.columnname === "开户银行") {
              //银行代码转名称
              k.value = this.YeepayBank.find(l => l.key === k.value)?.value;
            }
            if (k.datatype == 11) {
              // k.files =
            }
            if (i.isList) {
              //是列表
              obj[k.columnid] = k.value;
            }
          });
          tableData.push(obj);
        });
        tableDataObj[i.moudleTypeCode] = tableData;
      });
      mateArr.unshift({ name: "身份证", value: pic, icon: sfz });
      this.mateArr = mateArr;

      this.tableDataObj = tableDataObj;
      this.dataAll = data.data;
      this.info = info;
      this.loading = false;
    }
  },
  activated() {
    this.employeeId = this.$route.query.Employeeid;
    this.getData();
    // this.handleData(this.provinceCityList);
  },
  mounted() {
    // this.getData();
    // console.log(this.$route.query.Employeeid)
    this.handleData(this.provinceCityList);
    // :id="item.moudleTypeCode"
    // const codeDom = document.getElementById(`${this.$route.query.code}`);
    // const scrollDom = document.getElementsByClassName('el-form')[0];
    // const scrollDomInfo = scrollDom.getBoundingClientRect();
    // const codeDomInfo = codeDom.getBoundingClientRect();
    // scrollDom.scrollTop = codeDomInfo.top - scrollDomInfo.top;
  }
};
</script>

<style lang="less" scoped>
.employeeDetail {
  :deep(.el-table th.el-table__cell .cell) {
    color: #666666 !important;
  }
  display: flex;
  flex-direction: column;
  // align-items: center;
  // justify-content: center;
  :deep(.error) {
    font-size: 13px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f5f7fa;
    color: #c0c4d5;
  }
  .section {
    width: 990px;
    // min-width: 910px;
    margin: 0 auto;
    margin-top: 10px;
    box-shadow: 0px 0px 10px #d4dcf0;
    // border-radius: 8px;
    .user-info {
      display: flex;
      padding: 30px 30px;
      align-content: center;
      justify-content: space-between;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 70px;
          height: 70px;
          border-radius: 50%;
          .default {
            background: #51acee;
            line-height: 70px;
            font-size: 20px;
            border-radius: 50%;
            text-align: center;
            color: #fff;
          }
        }
        .info {
          margin-left: 20px;
          font-size: 13px;
          color: #999;

          .info1 {
            display: flex;
            align-items: flex-end;
            margin-bottom: 4px;
          }
          .name {
            font-size: 20px;
            font-weight: bold;
            margin-right: 20px;
            color: #333;
          }
        }
      }
      .oper {
        .btn {
          padding: 2px 5px;
          font-size: 14px;
          cursor: pointer;
          border: 1px solid #51acee;
          color: #51acee;
          border-radius: 4px;
          &:hover {
            transition: 0.3s all;
            background: #51acee;
            color: #fff;
          }
        }
      }
    }
    .nav {
      background: #f5f6fa;
      font-size: 13px;
      color: #333;
      display: flex;
      align-items: center;
      justify-content: center;
      .tab-item {
        padding: 17px 10px 15px 10px;
        margin: 0 5px;
        cursor: pointer;
        border-bottom: 2px solid #f5f6fa;
      }
      .tab-item-active {
        color: #51acee;
        border-bottom: 2px solid #51acee;
      }
    }
  }
  .section1 {
    margin-top: 20px;
    padding: 20px 30px;
    height: 500px;
    .bar {
      display: flex;
      align-items: center;
      // margin-bottom: 20px;
      .line {
        width: 3px;
        height: 16px;
        background: #51acee;
        border-radius: 3px;
        margin-right: 7px;
      }
      .text {
        font-weight: bold;
        font-size: 14px;
        color: #333;
      }
    }
    .form {
      padding: 8px 0px;
      font-size: 13px;
      display: flex;
      flex-wrap: wrap;
      .item {
        margin-top: 10px;
        width: 30%;
        display: flex;
        margin: 10px 8px 3px 8px;
        .label {
          color: #9f9f9f;
          width: 95px;
          text-align: right;
        }
      }
    }
    .table {
      margin-top: 20px;
    }
    .piccm {
      .pic-tab {
        :deep(.el-tabs) {
          width: 100%;
          .el-tabs__nav-wrap::after {
            background-color: white;
          }
        }
        :deep(.el-tabs__item) {
          .label_title {
            display: flex;
            flex-flow: row;
            align-items: center;
            justify-content: center;
            font-size: 15px;
          }
        }
        display: flex;
        margin-top: 20px;
        &-item {
          text-align: center;
          cursor: pointer;
          font-size: 14px;
          margin-right: 20px;
          img {
            width: 145px;
            height: 98px;
          }
          .txt {
            font-size: 13px;
          }
        }
        &-item-active {
          color: #51acee;
          img {
            box-shadow: 0px 0px 10px #d4dcf0;
          }
        }
      }
      .item {
        margin-top: 20px;
        .noneImg {
          display: flex;
          flex-flow: column;
          width: 180px;
          padding: 20px;
          align-items: center;
          justify-content: center;
          border: 1px dashed #d7d7d7;
          color: #d7d7d7;
          font-size: 12px;
          span {
            margin-top: 8px;
          }
        }
      }
    }
  }
}
</style>
